#include "can_rx.h"
#include "windows.h"
#include "ECanVci.h"
#include <string.h>
#include "adaptor_parm.h"
#include "operate_parm.h"
#include "can_rx.h"
#include "main.h"

int nDeviceType = 3; // USBCAN-I
int nDeviceInd = 0;
int nCANInd = 0;
DWORD dwRel;
CAN_OBJ vco;

#if 0
HANDLE mutex_can_tx = NULL;
string can_str_tmp;
char can_str_buf[64];
DWORD WINAPI can_rx(LPVOID pParam)
{
    int ret = 0;
    unsigned int cmd = 0;
    unsigned int jj = 0;
    unsigned int j = 0;
    unsigned char can_rx_buf[8];
    unsigned int src_cmd = 0;
    unsigned int src_operate_flag = 0;
    memset(can_rx_buf , 0 , sizeof(can_rx_buf));
    mutex_can_tx = CreateMutexA(NULL , false , NULL);;
    while(1)
    {
        memset(&vco , 0 , sizeof(vco));
        ret = Receive(4 , 0 , 0 , &vco, 1, 0xffffffff);//
        cmd = vco.ID & 0xfff;
        if( (100 <= cmd)  && (130 >= cmd))
        {
            j = cmd - 100;
            *(((unsigned long long*)(&adaptor_parm.byte[1]))+j) = *((unsigned long long*)vco.Data);
            adaptor_data_update();
        }

        if( (150 <= cmd)  && (200 >= cmd))
        {
            jj = cmd - 150;
            *(((unsigned long long*)(&operate_parm.power_operate_parm))+jj) = *((unsigned long long*)vco.Data);
            operate_data_update();
        }

        if(0x30e == cmd)
        {
            src_cmd = ((unsigned int)vco.Data[0])<<8 | (vco.Data[1]);
            src_operate_flag = vco.Data[2];
            if(0x201 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_5.lineedit_3->set_text(can_str_buf);
                can_str_tmp = to_string(src_operate_flag);
                lineedit_summary_5.lineedit_4->set_text((char*)can_str_tmp.c_str());
                lineedit_summary_5.lineedit_5->set_text("成功");
            }

            if(0x3 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_14.lineedit_1->set_text(can_str_buf);
                can_str_tmp = to_string(src_operate_flag);
                lineedit_summary_14.lineedit_2->set_text((char*)can_str_tmp.c_str());
                lineedit_summary_14.lineedit_3->set_text("成功");
            }

            if(0x1 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_6.lineedit_1->set_text(can_str_buf);
                can_str_tmp = to_string(src_operate_flag);
                lineedit_summary_6.lineedit_2->set_text((char*)can_str_tmp.c_str());
                lineedit_summary_6.lineedit_3->set_text("成功");
            }

            if(0x2 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_7.lineedit_1->set_text(can_str_buf);

                sprintf(can_str_buf , "0x%x" , src_operate_flag);
                lineedit_summary_7.lineedit_2->set_text(can_str_buf);
                lineedit_summary_7.lineedit_3->set_text("成功");
            }

            if(0x4 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_8.lineedit_1->set_text(can_str_buf);

                sprintf(can_str_buf , "0x%x" , src_operate_flag);
                lineedit_summary_8.lineedit_2->set_text(can_str_buf);
                lineedit_summary_8.lineedit_3->set_text("成功");
            }
        }

        if(0x30f == cmd)
        {
            src_cmd = ((unsigned int)vco.Data[0])<<8 | (vco.Data[1]);
            src_operate_flag = vco.Data[2];
            if(0x3 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_14.lineedit_1->set_text(can_str_buf);
                can_str_tmp = to_string(src_operate_flag);
                lineedit_summary_14.lineedit_2->set_text((char*)can_str_tmp.c_str());
                lineedit_summary_14.lineedit_3->set_text("失败");
            }

            if(0x1 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_6.lineedit_1->set_text(can_str_buf);

                sprintf(can_str_buf , "0x%x" , src_operate_flag);
                lineedit_summary_6.lineedit_2->set_text(can_str_buf);
                lineedit_summary_6.lineedit_3->set_text("失败");
            }

            if(0x2 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_7.lineedit_1->set_text(can_str_buf);

                sprintf(can_str_buf , "0x%x" , src_operate_flag);
                lineedit_summary_7.lineedit_2->set_text(can_str_buf);
                lineedit_summary_7.lineedit_3->set_text("失败");
            }

            if(0x4 == src_cmd)
            {
                can_str_tmp = to_string(src_cmd);
                sprintf(can_str_buf , "0x%x" , src_cmd);
                lineedit_summary_8.lineedit_1->set_text(can_str_buf);

                sprintf(can_str_buf , "0x%x" , src_operate_flag);
                lineedit_summary_8.lineedit_2->set_text(can_str_buf);
                lineedit_summary_8.lineedit_3->set_text("失败");
            }

        }

    }
}





DWORD WINAPI can_tx(LPVOID pParam)
{
    CAN_OBJ frame;
    union can_id_all can_id_tmp;
    memset(&frame,0,sizeof(frame));
    can_id_tmp.all = 0;
    can_id_tmp.part.src = 1;
    can_id_tmp.part.dst = 0;
    can_id_tmp.part.cmd = 0x102;
    frame.ID=can_id_tmp.all;
    frame.DataLen=8;
    frame.SendType=0;
    frame.RemoteFlag=0;
    frame.ExternFlag=1;
    char data[8];

    memset(data,0,sizeof(data));
    data[6] = 1;
    data[7] = 0xf;

    for(unsigned int i = 0 ; i < sizeof(pump_operate_parm)/8+1 ; i++)
    {
        memcpy(frame.Data , ((unsigned long long*)&pump_operate_parm) + i, frame.DataLen);

        if(Transmit(4, 0, 0,&frame,1) !=STATUS_OK)
        {
            MessageBoxA(NULL , "TX FAIL" , "FAIL", MB_OK);
        }
        can_id_tmp.part.cmd++;
    }

}

#endif
